package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdSensitiveServiceimpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNoneBlank(dto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }

        IPage<AdSensitive> pageRes = this.page(page, queryWrapper);

        //封装返回数据
        PageResponseResult result = new PageResponseResult((int) pageRes.getCurrent(), (int) pageRes.getSize(), (int) pageRes.getTotal());

        result.setData(pageRes.getRecords());

        return result;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //参数校验
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        adSensitive.setCreatedTime(new Date());

        this.save(adSensitive);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //参数校验
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives()) || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //根据ID查询
        AdSensitive sensitive = this.getById(adSensitive.getId());
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        adSensitive.setCreatedTime(new Date());
        //修改
        this.updateById(adSensitive);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据id进行删除
        this.removeById(id);

        return ResponseResult.okResult();
    }
}
